<template>
  <div v-bind="$attrs">
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      v-on="$listeners"
      @open="onOpen"
      @close="close"
      :title="dialogTitle"
    >
      <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
        <el-row>
          <el-col :span="8">
            <el-form-item label="菜单类型">
              <el-radio-group v-model="formData.type">
                <el-radio-button label="1">菜单</el-radio-button>
                <el-radio-button label="2">按钮</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col v-show="formData.type == 1" :span="8">
            <el-form-item label="菜单名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-col>
          <el-col v-show="formData.type == 2" :span="8">
            <el-form-item label="按钮名称">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="状态">
                <el-switch v-model="formData.status"> </el-switch>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-form-item label="菜单图标" v-show="formData.type == 1">
            <el-popover
              ref="popover"
              v-model="visiblePopover"
              placement="bottom"
              trigger="click"
              width="600"
            >
              <icon-group @editIcon="editIconHandle"></icon-group>
              <el-input
                v-model="formData.icon"
                slot="reference"
                @click="visible = !visible"
              >
              <i :class="'el-icon-' + formData.icon" slot="prefix" v-if="formData.icon" style="margin: 0 8px"/>
              <!-- <svg-icon v-if="formData.icon" slot="prefix" :icon-class="formData.icon" style="margin: 0 8px" class="el-input__icon" /> -->
              </el-input>
            </el-popover>
          </el-form-item>
          <el-row v-show="formData.type == 1">
            <el-col :span="8">
              <el-form-item label="外链菜单">
                <el-switch v-model="formData.outerLink"> </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="菜单缓存">
                <el-switch v-model="formData.cache"> </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="菜单可见">
                <el-switch v-model="formData.visible"> </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
        </el-row>
        <el-row>
          <el-col :span="12" v-show="formData.type == 1">
            <el-form-item label="路由地址">
              <el-input v-model="formData.routerLocation" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单排序" prop="sort">
              <el-input v-model="formData.sort"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="formData.type == 1">
          <el-col :span="12">
            <el-form-item label="组件名称">
              <el-input v-model="formData.componentName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组件路径">
              <el-input v-model="formData.componentPath" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="权限">
            <el-select v-model="formData.actionUid" multiple placeholder="请选择" filterable clearable>
              <el-option
                v-for="item in selectGroup"
                :key="item.uid"
                :label="item.actionKey"
                :value="item.uid">
              </el-option>
          </el-select>
        </el-form-item/>
        <el-form-item label="上级类目" v-if="this.isAdd">
          <Treeselect
            v-model="formData.pUid"
            :options="parentOption"
            :normalizer="normalizer"
          ></Treeselect>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end" style="margin-top: 50px;">
        <el-button type="primary" @click="submit">确认</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// import the component
import Treeselect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import iconGroup from "./iconGroup";
export default {
  components: { Treeselect, iconGroup },
  props: {
    dialogTitle: {
      type: String
    },
    parentOption: {
      type: Array
    },
    selectGroup: {
      type: Array
    },
  },
  data() {
    return {
      formData: {
        type: 1,
        name: "",
        sort: 99,
        pUid: null,
        icon: null,
        componentName: null,
        componentPath: null,
        outerLink: false,
        cache: false,
        status: true,
        visible: true,
        actionUid: []
      },
      rules: {
        name: [
          {required: true, message: '名称必填', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序必填', trigger: 'blur'},
          {type: 'number', message: '排序请使用数字', trigger: 'blur'}
        ]
      },
      visiblePopover: false,
      dialogVisible: false,
      jurisdiction: [],
      isAdd: true,
    };
  },
  methods: {
    onOpen() {},
    close() {
      this.$emit("update:visible", false);
    },
    normalizer(node) {
      return {
        id: node.uid,
        label: node.name,
        children: node.child
      };
    },
    editIconHandle(item) {
      console.log("更新的数据", item);
      this.$set(this.formData, "icon", item);
      this.visiblePopover = false;
    },
    submit() {
      const param = JSON.parse(JSON.stringify(this.formData));
      param.status == true ?  param.status= 1 :  param.status= 0;
      console.log(
        "%cMyProject%cline:134%cvar",
        "color:#fff;background:#ee6f57;padding:3px;border-radius:2px",
        "color:#fff;background:#1f3c88;padding:3px;border-radius:2px",
        "color:#fff;background:rgb(161, 23, 21);padding:3px;border-radius:2px",
        param
      );
      this.$refs.form.validate(valid=> {
        console.log(valid)
        if(!valid) return
        if(this.isAdd){
          this.$emit('addHandle', param)
        }else{
          this.$emit('editHandle', param)
        }
      })

    }
  }
};
</script>

<style></style>
